package com.industrial.im.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.aliyun.dysmsapi20170525.Client;
import com.aliyun.dysmsapi20170525.models.SendSmsRequest;
import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
import com.industrial.im.constant.SendResultConstant;
import com.industrial.im.service.SmsSenderService;
import com.industrial.im.vo.SendSmsVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Map;

@Service("aliyunSms")
@Slf4j
public class AliyunSmsSendServiceImpl implements SmsSenderService {

    @Resource
    private Client aliyunSmsClient ;

    @Value("${aliyun.sms.sign}")
    private String signName;
    /**
     * 发送短信
     *
     * @param contentMap
     * @return
     */
    @Override
    public void send(Map contentMap, SendSmsVo sendSmsVo) {
        String contentStr = JSONObject.toJSONString(contentMap);

        SendSmsRequest sendSmsRequest = new SendSmsRequest()
                .setPhoneNumbers(sendSmsVo.getPhone())
                .setTemplateCode(sendSmsVo.getTemplateCode())
                .setSignName(signName)
                .setTemplateParam(contentStr);
        try {
            SendSmsResponse sendSmsResponse = aliyunSmsClient.sendSms(sendSmsRequest);
            if (!StringUtils.equals(sendSmsResponse.body.code, SendResultConstant.OK)) {
                log.error("短信发送失败:{}", sendSmsResponse.body.getMessage());
            }

        } catch (Exception e) {
            log.error("短信发送失败:{}", e);
        }
    }
}
